package cn.itsource.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.system.domain.Logininfo;
import cn.itsource.system.domain.Menu;
import cn.itsource.system.domain.Payload;
import cn.itsource.system.domain.Permission;
import cn.itsource.system.mapper.LogininfoMapper;
import cn.itsource.system.mapper.MenuMapper;
import cn.itsource.system.mapper.PermissionMapper;
import cn.itsource.system.service.LogininfoService;
import cn.itsource.system.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LogininfoServiceImpl implements LogininfoService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * 参数校验
     * @param logininfo
     * @return
     */
    @Override
    public String login(Logininfo logininfo) {
        if(StrUtil.isBlank(logininfo.getUsername())){
            throw new RuntimeException("用户名不能为空!");
        }
        if(StrUtil.isBlank(logininfo.getPassword())){
            throw new RuntimeException("密码不能为空!");
        }
        if(logininfo.getType()==null){
            throw new RuntimeException("请传递有效参数!");
        }
        //通过用户名在数据库中查询是否存在
        Logininfo logininfoFromDB = logininfoMapper.selectByUsername(logininfo.getUsername());
        if (logininfoFromDB == null) {
            throw new RuntimeException("用户名不存在!");
        }
        //对前端传递过来的密码进行加密
        String password = SecureUtil.md5(logininfo.getPassword() + logininfoFromDB.getSalt());
        //如果密码比较不相等
        if(!logininfoFromDB.getPassword().equals(password)){
            throw new RuntimeException("密码错误!");
        }

        //根据当前登录人，查询拥有的权限列表
        List<Permission> permissions = permissionMapper.selectByLoginId(logininfoFromDB.getId());

        //根据当前登录人，查询拥有的菜单列表
        List<Menu> menus = menuMapper.selectByLoginId(logininfoFromDB.getId());

        //把列表转为集合
        menus = listTree(menus);

        //把登录信息，权限信息，菜单信息封装到PayLoad实体类里面
        Payload payload = new Payload(logininfoFromDB, permissions, menus);

        //产生token
        return JwtUtil.generateToken(payload);
    }

    private List<Menu> listTree(List<Menu> menus) {
        //查询所有的菜单列表
        List<Menu> menuList = menuMapper.selectAllType();
        //把list集合转为map
        Map<Long, Menu> map = menuList.stream().collect(Collectors.toMap(Menu::getId, m -> m));
        //我要返回的是树形菜单
        List<Menu> menuTree = new ArrayList<>();
        //对自己的菜单进行循环
        for (Menu menu : menus) {
            //获取父级菜单id
            Long parentId = menu.getParentId();
            //树形菜单，只拿到一级菜单的id
            List<Long> list = menuTree.stream().map(Menu::getId).toList();
            //获取一级菜单
            Menu parentMenu = map.get(parentId);
            if (parentMenu == null) {
                continue;
            }
            //一级菜单添加二级菜单
            parentMenu.getChildren().add(menu);
            //判断是否有父级id
            if(!list.contains(parentId)){
                menuTree.add(parentMenu);
            }
        }
        return menuTree;
    }


    public static void main(String[] args) {
        String s = SecureUtil.md5("123RaVhRTiPfy2i1uZq7rMHEXsZYAWzu3VA");
        System.out.println(s);
    }

}
